﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using YArchitech.LIB;
using HYRevitCode.FormUtility;
using QuickModel.RoomTransModel;

namespace QuickModel.Forms.RoomTransModelForms
{
	public partial class SetUpForm : System.Windows.Forms.Form
	{
		public List<RoomNameRef> RoomNameRefs { get; set; }

		public SetUpForm()
		{
			this.InitializeComponent();
			this.m_ctrlManager = new ControlInfoManager(this, new List<KeyValuePair<System.Windows.Forms.Control, bool>>(), null, null);
			this.m_lstRoomNameRef = RoomManager.GetRoomNameRefFromFile("");
		}

		private void SetUpForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.SetControlInfoToForm();
			}
			Dictionary<RoomNameKind, List<RoomNameRef>> dictionary = (from item in this.m_lstRoomNameRef
			group item by item.Kind).ToDictionary((IGrouping<RoomNameKind, RoomNameRef> item) => item.Key, (IGrouping<RoomNameKind, RoomNameRef> item) => item.ToList<RoomNameRef>());
			bool flag2 = dictionary.ContainsKey(RoomNameKind.eResidence);
			if (flag2)
			{
				this.initGridView(this.dgvResidence, dictionary[RoomNameKind.eResidence]);
			}
			else
			{
				this.initGridView(this.dgvResidence, new List<RoomNameRef>());
			}
			bool flag3 = dictionary.ContainsKey(RoomNameKind.eWork);
			if (flag3)
			{
				this.initGridView(this.dgvWork, dictionary[RoomNameKind.eWork]);
			}
			else
			{
				this.initGridView(this.dgvWork, new List<RoomNameRef>());
			}
			bool flag4 = dictionary.ContainsKey(RoomNameKind.eBusiness);
			if (flag4)
			{
				this.initGridView(this.dgvBusiness, dictionary[RoomNameKind.eBusiness]);
			}
			else
			{
				this.initGridView(this.dgvBusiness, new List<RoomNameRef>());
			}
			bool flag5 = dictionary.ContainsKey(RoomNameKind.eDining);
			if (flag5)
			{
				this.initGridView(this.dgvCatering, dictionary[RoomNameKind.eDining]);
			}
			else
			{
				this.initGridView(this.dgvCatering, new List<RoomNameRef>());
			}
			bool flag6 = dictionary.ContainsKey(RoomNameKind.eCustom);
			if (flag6)
			{
				this.initGridView(this.dgvUser, dictionary[RoomNameKind.eCustom]);
			}
			else
			{
				this.initGridView(this.dgvUser, new List<RoomNameRef>());
			}
		}

		private void butOK_Click(object sender, EventArgs e)
		{
			this.RoomNameRefs = new List<RoomNameRef>();
			this.RoomNameRefs.AddRange(this.GetRoomNameFormGrid(RoomNameKind.eResidence, this.dgvResidence));
			this.RoomNameRefs.AddRange(this.GetRoomNameFormGrid(RoomNameKind.eWork, this.dgvWork));
			this.RoomNameRefs.AddRange(this.GetRoomNameFormGrid(RoomNameKind.eBusiness, this.dgvBusiness));
			this.RoomNameRefs.AddRange(this.GetRoomNameFormGrid(RoomNameKind.eDining, this.dgvCatering));
			this.RoomNameRefs.AddRange(this.GetRoomNameFormGrid(RoomNameKind.eCustom, this.dgvUser));
			RoomManager.SaveRoomNameRefInFile(this.RoomNameRefs, "");
			base.DialogResult = DialogResult.OK;
		}

		private void butCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private List<RoomNameRef> GetRoomNameFormGrid(RoomNameKind eKind, DataGridView dataGridView)
		{
			List<RoomNameRef> list = new List<RoomNameRef>();
			DataGridViewRowCollection rows = dataGridView.Rows;
			for (int i = 0; i < rows.Count; i++)
			{
				DataGridViewCellCollection cells = rows[i].Cells;
				for (int j = 0; j < cells.Count; j++)
				{
					bool flag = cells[j].Value == null;
					if (!flag)
					{
						string text = cells[j].Value as string;
						bool flag2 = text.IsEmpty<char>();
						if (!flag2)
						{
							bool flag3 = cells[j].OwningColumn.HeaderText == "房间名称";
							if (flag3)
							{
								list.Add(new RoomNameRef
								{
									Name = text,
									Kind = eKind
								});
							}
						}
					}
				}
			}
			return list;
		}

		private void initGridView(DataGridView dataGridView, List<RoomNameRef> lstRoomNameRef)
		{
			dataGridView.Rows.Clear();
			DataGridViewColumnCollection columns = dataGridView.Columns;
			int count = columns.Count;
			int num = 0;
			for (int i = 0; i < columns.Count; i++)
			{
				bool flag = columns[i].HeaderText == "房间名称";
				if (flag)
				{
					num++;
				}
			}
			int num2 = Math.Max(51, (int)Math.Ceiling((double)lstRoomNameRef.Count / 3.0));
			dataGridView.Rows.Add(num2);
			int num3 = 0;
			int num4 = 1;
			for (int j = 0; j < num2; j++)
			{
				DataGridViewRow dataGridViewRow = dataGridView.Rows[j];
				for (int k = 0; k < count; k++)
				{
					DataGridViewCell dataGridViewCell = dataGridViewRow.Cells[k];
					bool flag2 = columns[k].HeaderText == "";
					if (flag2)
					{
						dataGridViewCell.Value = num4;
						num4++;
					}
					else
					{
						bool flag3 = columns[k].HeaderText == "房间名称" && num3 < lstRoomNameRef.Count;
						if (flag3)
						{
							dataGridViewCell.Value = lstRoomNameRef[num3].Name;
							num3++;
						}
					}
				}
			}
		}

		private void SetUpForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.GetControlInfoToXml();
			}
		}

		private const string c_strRoomNameColumnTitle = "房间名称";

		private const string c_strNumberColumnTitle = "";

		private const int c_nGridViewMinRowCount = 51;

		private List<RoomNameRef> m_lstRoomNameRef = new List<RoomNameRef>();

		private ControlInfoManager m_ctrlManager = null;
	}
}
